package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfonRedisService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.repostiory.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository repository;
    @Autowired
    private IUserInfonRedisService userInfonRedisService;


    @Override
    public UserInfo getOne(String id) {

        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return repository.findAll();
    }

    @Override
    public void upDate(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId("");
        repository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        UserInfo u = repository.findByPhone(phone);
        return u != null;
    }

    /**
     * 生成验证码发送短信并保存在redis中
     *
     * @param phone
     */
    @Override
    public void sendVerifyCode(String phone,String smsUrl,String appkey) {
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);


       StringBuilder builder = new StringBuilder(80);
       builder.append("你的验证码是:").append(code).append("请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");

      System.out.println(builder);

        /*RestTemplate restTemplate = new RestTemplate();
        String ret = restTemplate.getForObject(smsUrl, String.class, phone, "【DAIJIN】" + builder.toString(), appkey);
        System.out.println(ret);
        if (!ret.contains("Success")){
            throw  new LogicException("短息发送失败");
        }*/

        //保存验证码
        userInfonRedisService.setVerifyCode(phone, code);
    }

    //参数校验
    @Override
    public void regist(String phone, String nickname, String password, String rpssword, String verifyCode) {
        //判空校验
        AssertUtil.hasLength(phone, "手机号不能为空");
        AssertUtil.hasLength(nickname, "昵称不能是空");
        AssertUtil.hasLength(password, "密码不能是空");
        AssertUtil.hasLength(rpssword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");

        AssertUtil.isEquals(password, rpssword, "输入密码不一致");
        //当前对象调方法userInfoService
        if (this.checkPhone(phone)) {
            throw new LogicException("手机号已经存在了,请直接登录");
        }
        //redis中查验证码
        String code = userInfonRedisService.getVerifyCode(phone);
        if (code==null|| !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码失效,或者输入错误");
        }
        //以上参数判断结束执行注册添加
        UserInfo userInfo = new UserInfo();
        userInfo. setNickname(nickname) ;
        userInfo. setPhone (phone);
        userInfo. setEmail("");
        userInfo. setPassword(password);
        userInfo. setLevel(1);
        userInfo. setCity("");
        userInfo. setHeadImgUrl("/images/default. jpg");
        userInfo.setInfo("");
        userInfo.setState(UserInfo.STATE_NORMAL);

        this.save(userInfo);


    }


    @Override
    public UserInfo login(String username, String password) {
        UserInfo user = repository.findByPhone(username);

        if (user==null || !password.equals(user.getPassword())){
            throw new  LogicException("账号或者密码错误");
        }


        return user;
    }

    @Override
    public List<UserInfo> queryByCity(String keyword) {
        return repository.queryByCity(keyword);
    }
}


































































